package com.lwj.service.configuration;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lwj.dto.configuration.UserRoleDto;
import com.lwj.dto.configuration.UserRoleListDto;
import com.lwj.mapper.configuration.UserRoleMapper;
import com.lwj.model.administration.Department;
import com.lwj.model.configuration.UserRole;
import com.lwj.model.configuration.UserRoleKey;
import com.lwj.vo.PageBean;
import com.lwj.vo.configuration.RoleVo;
import com.lwj.vo.configuration.UserPermissionVo;
import com.lwj.vo.configuration.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleServiceImpl implements UserRoleService{
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public int deleteByPrimaryKey(UserRoleKey key) {
        return userRoleMapper.deleteByPrimaryKey(key);
    }

    @Override
    public int insert(UserRole record) {
        return userRoleMapper.insert(record);
    }

    @Override
    public int insertSelective(UserRole record) {
        return userRoleMapper.insertSelective(record);
    }

    @Override
    public UserRole selectByPrimaryKey(UserRoleKey key) {
        return userRoleMapper.selectByPrimaryKey(key);
    }

    @Override
    public int updateByPrimaryKeySelective(UserRole record) {
        return userRoleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(UserRole record) {
        return userRoleMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageBean<UserRoleVo> getUserByRole(UserRoleListDto dto) {
        Page<UserRoleVo> objects = PageHelper.startPage(dto.getPageNumber(), dto.getPageSize());
        userRoleMapper.getUserByRole(dto);
        return PageBean.page(objects);
    }

    @Override
    public void addUserToRole(UserRoleDto dto) {
        String departmentRoleId = dto.getDepartmentRoleId();
        List<String> empIds = dto.getEmpIds();
        for (int i = 0; i < empIds.size(); i++) {
            String empId = empIds.get(i);
            UserRole userRole = new UserRole();
            userRole.setDepartmentRoleId(departmentRoleId);
            userRole.setEmpId(empId);
            userRoleMapper.insertSelective(userRole);
        }
    }

    @Override
    public void deleteUserRole(String departmentRoleId, String empId) {
        userRoleMapper.deleteByPrimaryKey(new UserRoleKey(empId,departmentRoleId));
    }

    @Override
    public List<UserPermissionVo> selectUserPermission(String empId, String permissionCode) {
        return userRoleMapper.selectUserPermission(empId,permissionCode);
    }


}
